package com.boc.langxiang.presenter.shop;

import com.boc.langxiang.contract.shop.SpecialListView;
import com.boc.langxiang.entity.request.GetSpecialListRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.SpecialListContent;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Li Guoxi on 2016/6/28.
 */
public class SpecialListPresenter {
    private SpecialListView mView;

    private final ApiService mService;

    public SpecialListPresenter(SpecialListView mView) {
        this.mView = mView;
        this.mService = NetClient.getInstance().net().create(ApiService.class);
    }

    public void getList() {
        GetSpecialListRequest request = new GetSpecialListRequest();
        request.setPage(1);
        mView.showLoading();
        Observable<BaseResponse<SpecialListContent>> observable = mService.getSpecialList(request.params());
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<SpecialListContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<SpecialListContent> response) {
                        mView.getListSuccess(response);
                        mView.hideLoading();
                    }
                });
    }

    public void loadMore(final int page) {
        GetSpecialListRequest request = new GetSpecialListRequest();
        request.setPage(page);
        Observable<BaseResponse<SpecialListContent>> observable = mService.getSpecialList(request.params());
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new HandleErrorSubscriber<BaseResponse<SpecialListContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<SpecialListContent> response) {
                        mView.loadMoreSuccess(response.getContent().getRes(), page);
                    }
                });
    }
}
